package com.yuexun.dhdoctor.module.personalPlan.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.personalPlan.adapter.MyScheduleAdapter;
import com.yuexun.dhdoctor.module.personalPlan.bean.ScheduleBean;
import com.yuexun.dhdoctor.module.personalPlan.bean.ScheduleData;
import com.yuexun.dhdoctor.module.personalPlan.model.impl.PlanModel;
import com.yuexun.dhdoctor.module.personalPlan.presenter.impl.SchedulePresenter;
import com.yuexun.dhdoctor.module.personalPlan.ui.Act_Schedule_Add;
import com.yuexun.dhdoctor.module.personalPlan.ui.view.IScheduleView;
import com.yuexun.dhdoctor.util.DateUtil;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/20 0020.
 */

public class Frag_MySchedule extends KJFragment implements IScheduleView {

    @Bind(R.id.schedule_list)
    ExpandableListView scheduleList;
    @Bind(R.id.tv_week_title)
    TextView tvWeekTitle;
    @Bind(R.id.tv_week_date)
    TextView tvWeekDate;


    Calendar calendar = Calendar.getInstance();
    List<ScheduleBean> scheduleBeen = new ArrayList<>();
    List<ScheduleData> datas = new ArrayList<>();
    MyScheduleAdapter adapter;
    SchedulePresenter presenter;
    String startDate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.frag_my_schedule, null);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        return null;
    }

    private void init(){
        presenter = new SchedulePresenter(new PlanModel(), this);
        scheduleList.setGroupIndicator(null);
        calendar.setTime(new Date());

        scheduleList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                try {
                    Long date = new SimpleDateFormat("yyyy-MM-dd").parse(scheduleBeen.get(i).getDate()).getTime();
                    String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
                    if(date < new SimpleDateFormat("yyyy-MM-dd").parse(today).getTime()){
                        ViewInject.toast("历史记录不能编辑！");
                    }else{
                        Intent intent_lock = new Intent(getActivity(), Act_Schedule_Add.class);
                        intent_lock.putExtra("type", "edit");
                        intent_lock.putExtra("datetime", scheduleBeen.get(i).getDate());
                        intent_lock.putExtra("week", scheduleBeen.get(i).getWeek());
                        intent_lock.putExtra("content", scheduleBeen.get(i).getDatas().get(i1).getContent());
                        intent_lock.putExtra("remindTime", scheduleBeen.get(i).getDatas().get(i1).getRemindTime());
                        intent_lock.putExtra("scheduleId", scheduleBeen.get(i).getDatas().get(i1).getScheduleId());
                        getActivity().startActivity(intent_lock);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                return true;
            }
        });
    }

    @OnClick({R.id.task_back, R.id.task_now, R.id.task_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.task_back:
                calendar.add(Calendar.DATE, -7);
                set();
                break;
            case R.id.task_now:
                calendar.setTime(new Date());
                set();
                break;
            case R.id.task_next:
                calendar.add(Calendar.DATE, 7);
                set();
                break;
        }
    }

    private void set() {
        setDateTitle();
        setDate(calendar);
    }

    private void setDate(Calendar calendar) {
        scheduleBeen.clear();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(calendar.getTime());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        calendar1 = DateUtil.getMondayOfThisWeek_Calendar(calendar1);

        startDate = formatter.format(calendar1.getTime());

        for (int i = 0; i < 7; i++) {
            ScheduleBean scheduleBean = new ScheduleBean();
            scheduleBean.setDatas(datas);
            scheduleBean.setMonth(new SimpleDateFormat("MM-dd", Locale.getDefault()).format(calendar1.getTime()));
            scheduleBean.setDate(formatter.format(calendar1.getTime()));
            if (i == 6) {
                scheduleBean.setWeek("周日");
            }else if (i == 0) {
                scheduleBean.setWeek("周一");
            }else if (i == 1) {
                scheduleBean.setWeek("周二");
            }else if (i == 2) {
                scheduleBean.setWeek("周三");
            }else if (i == 3) {
                scheduleBean.setWeek("周四");
            }else if (i == 4) {
                scheduleBean.setWeek("周五");
            }else if (i == 5) {
                scheduleBean.setWeek("周六");
            }
            calendar1.add(Calendar.DATE, 1);

            scheduleBeen.add(scheduleBean);
        }

        clear_list();
        clear_date();
    }

    private void clear_list(){
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("startDate", startDate);
        presenter.getScheduleList(params);
    }

    private void clear_date() {
        if(adapter == null){
            adapter = new MyScheduleAdapter(getActivity(), scheduleBeen);
            scheduleList.setAdapter(adapter);

            for(int i = 0; i < adapter.getGroupCount(); i++){
                scheduleList.expandGroup(i);
            }
        }else{
            adapter.setScheduleBeen(scheduleBeen);
        }
    }

    private void setDateTitle() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月", Locale.getDefault());
        int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
        tvWeekTitle.setText(formatter.format(calendar.getTime()));
        tvWeekDate.setText("第" + weekOfMonth + "周(" +
                DateUtil.getMondayOfThisWeek_Title(calendar) + "-" + DateUtil.getSundayOfThisWeek_Title(calendar) + ")");
    }

    @Override
    public void getScheduleList(BaseEntity<Map<String , List<ScheduleData>>> baseEntity) {
        if(baseEntity.getStatus().equals("200")){
            List<Map<String , List<ScheduleData>>> maps = baseEntity.getDatas();
            for(String date : maps.get(0).keySet()){
                List<ScheduleData> scheduleDatas =  maps.get(0).get(date);
                if(scheduleDatas != null && scheduleDatas.size() > 0){
                    for(ScheduleBean scheduleBean : scheduleBeen){
                        if(scheduleBean.getDate().equals(date)){
                            scheduleBean.setDatas(scheduleDatas);
                        }
                    }
                }
            }
            clear_date();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        set();
    }
}
